package control;

import interceptor.InjectConnection;

import java.sql.Connection;
import java.util.List;
import java.util.ArrayList;

import utils.Var;
import utils.TextUtils;
import examinee.ExamineeInfo;
import examinee.Examinee;
import exam.TestPaperInfo;
import exam.TestPaper;
import exam.ExamVar;
import account.AccountUtils;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-6
 * Time: 21:37:39
 */
public class ComputerAction {

  private Connection con;

  @InjectConnection
  public void setCon(Connection con) {
    this.con = con;
  }

  private List<ComputerInfo> list;

  public String getComputerList() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_CONTROL_IP | Var.RIGHT_CONTROL_EXAMINEE))
      throw new Exception("deny!");

    list = Computer.getComputerList(con);
    for (ComputerInfo info : list) {
      ExamineeInfo einfo = Examinee.getExaminee(con, info.getZkzh(), TextUtils.getToday());
      info.setEname(einfo.getEname());
      info.setZkzh(einfo.getZkzh());

      if (info.getState() == ControlVar.COMPUTER_STATE_INUSE) {
        TestPaperInfo tinfo = TestPaper.getTestPaper(con, einfo.getEid());
        if (tinfo.getTid() == 0) info.setState(ControlVar.COMPUTER_STATE_IDEL);
        if (tinfo.getState() == ExamVar.STATE_END) info.setState(ControlVar.COMPUTER_STATE_DOME);
      }
    }
    return Var.ACTION_SUCCESS;
  }

  public List<ComputerInfo> getList() {
    return list;
  }

  private ComputerInfo computer;

  public String setComputerIP() throws Exception {
    ComputerInfo current = Computer.getComputer(con, computer.getCid());
    current.setIp(computer.getIp());
    Computer.modiComputer(con, computer);
    return Var.ACTION_SUCCESS;
  }

  public String setComputerUser() throws Exception {
    ComputerInfo current = Computer.getComputer(con, computer.getCid());

    //check computer state
    if (current.getState() == ControlVar.COMPUTER_STATE_INUSE) {
      ExamineeInfo einfo = Examinee.getExaminee(con, current.getZkzh(), TextUtils.getToday());
      TestPaperInfo tinfo = TestPaper.getTestPaper(con, einfo.getEid());
      if (tinfo.getTid() != 0 && tinfo.getState() != ExamVar.STATE_END) return Var.ACTION_ERROR;
    }

    if (computer.getState() == ControlVar.COMPUTER_STATE_IDEL || computer.getState() == ControlVar.COMPUTER_STATE_MAINTAIN)
      computer.setZkzh("");
    else if (computer.getZkzh().trim().equals(""))
      computer.setState(ControlVar.COMPUTER_STATE_IDEL);
    else {
      ComputerInfo c = Computer.getComputerByZkzh(con, computer.getZkzh());
      if (c.getCid() != 0)
        computer = current;
      else
        computer.setState(ControlVar.COMPUTER_STATE_ARRANGE);
    }

    computer.setIp(current.getIp());
    Computer.modiComputer(con, computer);
    return Var.ACTION_SUCCESS;
  }

  public ComputerInfo getComputer() {
    return computer;
  }

  public void setComputer(ComputerInfo computer) {
    this.computer = computer;
  }

  private int cid;

  public String getComputerInfo() throws Exception {
    computer = Computer.getComputer(con, cid);

    ExamineeInfo einfo = Examinee.getExaminee(con, computer.getZkzh(), TextUtils.getToday());
    TestPaperInfo tinfo = TestPaper.getTestPaper(con, einfo.getEid());
    if (einfo.getEid() == 0 || tinfo.getState() == ExamVar.STATE_END)
      computer.setZkzh("");

    return Var.ACTION_SUCCESS;
  }

  public void setCid(int cid) {
    this.cid = cid;
  }

  private String zkzh;
  private List<ExamineeInfo> elist;

  public String getExaminees() throws Exception {
    String zkzhs = "|";
    List<ComputerInfo> computers = Computer.getComputerList(con);
    for (ComputerInfo computer : computers) {
      if (computer.getZkzh() == null || computer.getZkzh().trim().length() == 0) continue;
      zkzhs += computer.getZkzh() + "|";
    }

    elist = new ArrayList<ExamineeInfo>();
    List<ExamineeInfo> all = Examinee.getTodayExamineeList(con, zkzh);
    for (ExamineeInfo e : all) {
      if (zkzhs.indexOf("|" + e.getZkzh() + "|") == -1) elist.add(e);
    }
    return Var.ACTION_SUCCESS;
  }

  public void setZkzh(String zkzh) {
    this.zkzh = zkzh;
  }

  public List<ExamineeInfo> getElist() {
    return elist;
  }
}
